#include<stdio.h>
#include "taskqueue.h"
#include<stdlib.h>

bool isEmpty(TaskQueue* taskqueue) {
	return taskqueue->rear == NULL;
}

TaskQueue* initTaskQueue()
{	
	printf("init");
	TaskQueue* taskqueue = (TaskQueue*)malloc(sizeof(TaskQueue));
	taskqueue->rear = NULL;
	taskqueue->front = NULL;
	taskqueue->currentsize = 0;
	return taskqueue;
}

void destoryTaskQueue(TaskQueue* taskqueue)
{	
	Task* pre =  taskqueue->front,*p;
	printf("start clean some tasks...");
	if (pre!=NULL)
	{
		p = pre->next;
		while (p!=NULL)
		{
			free(pre);
			pre = p; p = p->next;
		}
		free(pre);
	}
	printf("tasks have been clean...");
	free(taskqueue);
	
	taskqueue = NULL;
	printf("taskqueue have been clean...");
}

Task* getTask(TaskQueue* taskqueue)
{
	if (isEmpty(taskqueue))
	{
		return NULL;
	}
	Task* p = taskqueue->front;
	if (taskqueue->front == taskqueue->rear)
	{
		taskqueue->front = NULL;
		taskqueue->rear = NULL;
	}
	else
	{
		taskqueue->front = p->next;
	}
	
	taskqueue->currentsize--;
	return p;
}

void addTask(TaskQueue* taskqueue, Task* task)
{
	printf("add");
	if (isEmpty(taskqueue))
	{
		taskqueue->rear = task;
		taskqueue->front = task;
	}
	else
	{
		taskqueue->rear->next = task;
		taskqueue->rear = task;
	}
	task->next = NULL;
	taskqueue->currentsize++;
}
